

#import "XLCycleScrollView.h"


@interface XLCycleScrollView (){
    
}

@property(strong,nonatomic)UITextField *selecttextField;

@end



@implementation XLCycleScrollView

@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;
@synthesize currentPage = _curPage;
/*
- (void)dealloc
{
    [_scrollView release];
    [_pageControl release];
    [_curViews release];
    [super dealloc];
}*/

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(self.bounds.size.width * 3, self.bounds.size.height);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        _scrollView.pagingEnabled = YES;
        [self addSubview:_scrollView];
        
        CGRect rect = self.bounds;
        rect.origin.y = rect.size.height - 10;
        rect.size.height = 10;
        _pageControl = [[UIPageControl alloc] initWithFrame:rect];
        _pageControl.userInteractionEnabled = NO;
        
        [self addSubview:_pageControl];
        
        _curPage = 0;
        
    }
    return self;
}

- (void)setDataource:(id<XLCycleScrollViewDatasource>)datasource
{
    _datasource = datasource;
    [self reloadData];
}

- (void)reloadData
{
    _totalPages = [_datasource xlscrollNumberOfPages:self];
    if (_totalPages == 0) {
        return;
    }
    _pageControl.numberOfPages = _totalPages;
    [self loadData];
}

- (void)loadData
{
    
    _pageControl.currentPage = _curPage;
    
    //从scrollView上移除所有的subview
//    NSArray *subViews = [_scrollView subviews];
//    if([subViews count] != 0) {
//        [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    }
    
    [self getDisplayImagesWithCurpage:_curPage];
    
   
    
    [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0)];
}

- (void)getDisplayImagesWithCurpage:(int)page {
    
    int pre = [self validPageValue:_curPage-1];
    int last = [self validPageValue:_curPage+1];
    
    if (!_curViews) {
        _curViews = [[NSMutableArray alloc] init];
    }
    
    [_curViews removeAllObjects];
    
    [_curViews addObject:[_datasource xlscroll:self pageAtIndex:pre]];
    [_curViews addObject:[_datasource xlscroll:self pageAtIndex:page]];
    [_curViews addObject:[_datasource xlscroll:self pageAtIndex:last]];
}

- (int)validPageValue:(NSInteger)value {
    
    if(value == -1) value = _totalPages - 1;
    if(value == _totalPages) value = 0;
    
    return value;
    
}

- (void)handleTap:(UITapGestureRecognizer *)tap {
    
    if ([_delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
        [_delegate didClickPage:self atIndex:_curPage];
    }
    
}

- (void)setViewContent:(UIView *)view atIndex:(NSInteger)index
{
    if (index == _curPage) {
        [_curViews replaceObjectAtIndex:1 withObject:view];
        for (int i = 0; i < 3; i++) {
            UIView *v = [_curViews objectAtIndex:i];
            v.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                        action:@selector(handleTap:)];
            [v addGestureRecognizer:singleTap];
            //[singleTap release];
            v.frame = CGRectOffset(v.frame, v.frame.size.width * i, 0);
            [_scrollView addSubview:v];
        }
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    int x = aScrollView.contentOffset.x;
    
    //往下翻一张
    if(x >= (2*self.frame.size.width)) {
        _curPage = [self validPageValue:_curPage+1];
        [self loadData];
    }
    
    //往上翻
    if(x <= 0) {
        _curPage = [self validPageValue:_curPage-1];
        [self loadData];
    }
    
//    
//    BOOL directRight;
//    
//    if (previousOffSet.x < aScrollView.contentOffset.x) {
//        
//        directRight = YES;
//        
//    }
//    
//    else{
//        
//        directRight = NO;
//        
//    }
//    
//    previousOffSet.x = aScrollView.contentOffset.x;
//    
//    //防止最开始就向上面拖动的时候，改变数组视图树的位置。
//    
//    if (aScrollView.contentOffset.y < 0) {
//        
//        return;
//        
//    }
//    
//    if (directDown) {
//        
//        NSLog(@"down");
//        
//        MyView * subView = [_aryViews objectAtIndex:firstViewIndex];
//        
//        CGFloat firstViewYOffset = subView.frame.origin.y + subView.frame.size.height + sMyViewGap;
//        
//        //寻找第一个视图是否滚动出去
//        
//        if (firstViewYOffset < aScrollView.contentOffset.y) {
//            
//            //改变数组中第一排可见视图的位置。
//            
//            [self moveIndexInViewsWithDirect:YES];
//            
//        }
//        
//    }
//    
//    else{
//        
//        NSLog(@"up");
//        
//        MyView * subView = [_aryViews objectAtIndex:(firstViewIndex + sMyViewTotal - 2)%sMyViewTotal];
//        
//        CGFloat lastViewYOffset = subView.frame.origin.y - scrollView.bounds.size.height;
//        
//        if (lastViewYOffset > scrollView.contentOffset.y) {
//            
//            [self moveIndexInViewsWithDirect:NO];
//            
//        }
//        
//    }
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView {
    [_scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width, 0) animated:YES];
}

- (void)moveIndexInViewsWithDirect:(BOOL)forward{
    
//    [UIView setAnimationsEnabled:NO];
//    
//    if (forward) {
//        
//        for (int i = firstViewIndex; i < (firstViewIndex + 2); i++) {
//            
//            MyView *subView = [_aryViews objectAtIndex:i%sMyViewTotal];
//            
//            subView.showNumber = subView.showNumber + sMyViewTotal;
//            
//            subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + (sMyViewTotal/2) * (sMyViewHeight + sMyViewGap), subView.frame.size.width, subView.frame.size.height);
//            
//        }
//        
//        firstViewIndex = (firstViewIndex + 2)%sMyViewTotal;
//        
//    }
//    
//    else{
//        
//        int lastViewIndex = firstViewIndex + sMyViewTotal - 1;
//        
//        for (int i = lastViewIndex; i > (lastViewIndex - 2); i--) {            MyView *subView = [_aryViews objectAtIndex:(firstViewIndex + sMyViewTotal - i)%sMyViewTotal];
//            
//            subView.showNumber = subView.showNumber - sMyViewTotal;
//            
//            subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y - (sMyViewTotal/2) * (sMyViewHeight + sMyViewGap), subView.frame.size.width, subView.frame.size.height);
//            
//        }
//        
//        firstViewIndex = (firstViewIndex + sMyViewTotal - 2)%sMyViewTotal;
//        
//    }  
//    
//    [UIView setAnimationsEnabled:YES];  
    
}


@end
